#ifndef RINGBUFFER_HPP
#define RINGBUFFER_HPP

#include <vector>
#include <string>

struct Data {
    std::string data;
    int client_fd;
};

class RingBuffer {
public:
    RingBuffer(int size, int efd)
    :   _buffer(size), _size(size), _efd(efd)
    { }
    ~RingBuffer(){
    }

    bool push(std::string&& data, int client_fd){
        if(full()) return false;
        _buffer[_tail].data = std::move(data);
        _buffer[_tail].client_fd = client_fd;
        _tail = (_tail + 1) & (_size - 1);
        if (_efd != 0) {
            write(_efd, &value, sizeof(value)); // 触发epoll事件
            std::cout << "trigger epoll event" << std::endl;
        }
        return true;
    }
    bool pop(std::string& data, int& client_fd){
        if(empty()) return false;
        data = std::move(_buffer[_head].data);
        client_fd = _buffer[_head].client_fd;
        _head = (_head + 1) & (_size - 1);
        return true;
    }
    bool empty(){
        return _head == _tail;
    }
    bool full(){
        return (_tail + 1) % _size == _head;
    }
    int size(){
        return _buffer.size();
    }
    int getFd(){
        return _efd;
    }

private:
    std::vector<Data> _buffer;
    int _head{0};
    int _tail{0};
    int _size;
    int _efd;
    uint64_t value = 1;
};

#endif // RINGBUFFER_HPP